﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace WebApplication1
{
    public partial class TrangChu : System.Web.UI.Page
    {
        sv_QuanLyNhaSach.AdapterClient service = new sv_QuanLyNhaSach.AdapterClient();
        string MaTKOL = "";
        PagedDataSource pds = new PagedDataSource();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Browser.Cookies)
            {
                if (Request.Cookies.Get("Login") != null)
                {
                    HttpCookie MyCookies = Request.Cookies["Login"];
                    if (MyCookies["UserName"] != "" || MyCookies["UserName"] != null)
                    {
                        MaTKOL = LayMaTK(MyCookies["UserName"].ToString());
                    }
                }
                else
                    if ( Session["UserName"] != null)
                    {
                        MaTKOL = LayMaTK(Session["UserName"].ToString());
                    }
            }
            
            Load_Sach();
            Load_SachMoi();
            //Load_SanPham();          
        }

        //private void Load_SanPham()
        //{
        //    pds.AllowPaging = true;
        //    pds.PageSize = 1;
        //    pds.CurrentPageIndex = 0;
        //    DataSet ds = new DataSet();
        //    ds.ReadXml(new StringReader(service.selectqueryByText("select MaMH, Hinh,TenMH, DonGiaLe,Khuyenmai from MatHang where MaMH not like 'SA%'")));
        //    if (ds.Tables.Count > 0)
        //    {
        //        pds.DataSource = ds.Tables[0].DefaultView;
        //        for (int i = 0; i < pds.PageCount; i++)
        //        {
        //            LinkButton btn = new LinkButton();
        //            btn.Text = (i + 1).ToString();
        //            btn.Click += new EventHandler(btn_Click);
        //            btn.Width = 15;
        //            Panel3.Controls.Add(btn);
        //        }
        //        dl_sachmoi.DataSource = pds;
        //        dl_sachmoi.DataBind();
        //    }                
            
        //}

        void btn_Click(object sender, EventArgs e)
        {
            int trang = int.Parse(((LinkButton)sender).Text) - 1;
            pds.CurrentPageIndex = trang;
            dl_sachgk.DataSource = pds;
            dl_sachgk.DataBind();
        }
        private string LayMaTK(string p)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select matkol from TaiKhoanOnline where TenDangNhap='" + p + "' ")));
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0].Rows[0][0].ToString();
            }
            return "";
        }
        private void Load_Sach()
        {
            //pds.AllowPaging = true;
            //pds.PageSize = 10;
            //pds.CurrentPageIndex = 0;
            //ds.ReadXml(new StringReader(service.selectqueryByText("select MaMH,TenMH, DonGiaLe,Khuyenmai from MatHang where MaMH like 'SAGK%'")));
            //if (ds.Tables.Count > 0)
            //{
            //    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            //    {
            //        if (ds.Tables[0].Rows[i]["DonGiaLe"].ToString().Contains(".0000") == true)
            //        {
            //            ds.Tables[0].Rows[i]["DonGiaLe"] = ds.Tables[0].Rows[i]["DonGiaLe"].ToString().Replace(".0000", "");
            //        }
            //    }
            //    pds.DataSource = ds.Tables[0].DefaultView;
            //    for (int i = 0; i < pds.PageCount; i++)
            //    {
            //        LinkButton btn = new LinkButton();
            //        btn.Text = (i + 1).ToString();
            //        btn.Click += new EventHandler(btn_Click);
            //        btn.Width = 15;
            //        Panel3.Controls.Add(btn);
            //    }
            //    if (!IsPostBack)
            //    {
            //        dl_sachgk.DataSource = pds;
            //        dl_sachgk.DataBind();
            //    }
            //}
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select top 5 MaMH,TenMH, DonGiaLe,Khuyenmai from MatHang where MaMH like 'SAGK%'")));
            if (ds.Tables.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i]["DonGiaLe"] = double.Parse(ds.Tables[0].Rows[i]["DonGiaLe"].ToString()).ToString("#,##0"); 
                } 
                if (!IsPostBack)
                {
                    dl_sachgk.DataSource = ds.Tables[0];
                    dl_sachgk.DataBind();
                }
            }
        }
        private void Load_SachMoi()
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("MaMH", typeof(string));
            tbl.Columns.Add("TenMH", typeof(string));
            tbl.Columns.Add("TacGia", typeof(string));
            tbl.Columns.Add("NhaSX", typeof(string));
            tbl.Columns.Add("DonGia", typeof(double));
            tbl.Columns.Add("KhuyenMai", typeof(float));
            DataSet ds1 = new DataSet();
            ds1.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where mamh like '%TT%' order by mamh desc")));
            DataRow dr = tbl.NewRow();
            if (ds1.Tables.Count > 0)
            {
                dr["MaMH"] = ds1.Tables[0].Rows[0]["MaMH"];
                dr["TenMH"] = ds1.Tables[0].Rows[0]["TenMH"];
                dr["TacGia"] = ds1.Tables[0].Rows[0]["TacGia"];
                dr["NhaSX"] = ds1.Tables[0].Rows[0]["NhaSX"];
                dr["DonGia"] = double.Parse(ds1.Tables[0].Rows[0]["DonGiaLe"].ToString());
                dr["Khuyenmai"] = float.Parse(ds1.Tables[0].Rows[0]["Khuyenmai"].ToString());
                tbl.Rows.Add(dr);
            }
            DataSet ds2 = new DataSet();
            ds2.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where mamh like '%KT%' order by mamh desc")));
            dr = tbl.NewRow();
            if (ds2.Tables.Count > 0)
            {
                dr["MaMH"] = ds2.Tables[0].Rows[0]["MaMH"];
                dr["TenMH"] = ds2.Tables[0].Rows[0]["TenMH"];
                dr["TacGia"] = ds2.Tables[0].Rows[0]["TacGia"];
                dr["NhaSX"] = ds2.Tables[0].Rows[0]["NhaSX"];
                dr["DonGia"] = double.Parse(ds2.Tables[0].Rows[0]["DonGiaLe"].ToString());
                dr["Khuyenmai"] = float.Parse(ds2.Tables[0].Rows[0]["Khuyenmai"].ToString());
                tbl.Rows.Add(dr);
            }
            DataSet ds3 = new DataSet();
            ds3.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where mamh like '%KI%' order by mamh desc")));
            dr = tbl.NewRow();
            if (ds3.Tables.Count > 0)
            {
                dr["MaMH"] = ds3.Tables[0].Rows[0]["MaMH"];
                dr["TenMH"] = ds3.Tables[0].Rows[0]["TenMH"];
                dr["TacGia"] = ds3.Tables[0].Rows[0]["TacGia"];
                dr["NhaSX"] = ds3.Tables[0].Rows[0]["NhaSX"];
                dr["DonGia"] = double.Parse(ds3.Tables[0].Rows[0]["DonGiaLe"].ToString());
                dr["Khuyenmai"] = float.Parse(ds3.Tables[0].Rows[0]["Khuyenmai"].ToString());
                tbl.Rows.Add(dr);
            }
            DataSet ds4 = new DataSet();
            ds4.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where mamh like '%CN%' order by mamh desc")));
            dr = tbl.NewRow();
            if (ds4.Tables.Count > 0)
            {
                dr["MaMH"] = ds4.Tables[0].Rows[0]["MaMH"];
                dr["TenMH"] = ds4.Tables[0].Rows[0]["TenMH"];
                dr["TacGia"] = ds4.Tables[0].Rows[0]["TacGia"];
                dr["NhaSX"] = ds4.Tables[0].Rows[0]["NhaSX"];
                dr["DonGia"] = double.Parse(ds4.Tables[0].Rows[0]["DonGiaLe"].ToString());
                dr["Khuyenmai"] = float.Parse(ds4.Tables[0].Rows[0]["Khuyenmai"].ToString());
                tbl.Rows.Add(dr);
            }
            DataSet ds5 = new DataSet();
            ds5.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaMH,TenMH,TacGia,NhaSX, DonGiaLe,Khuyenmai from MatHang where mamh like '%NN%' order by mamh desc")));
            dr = tbl.NewRow();
            if (ds5.Tables.Count > 0)
            {
                dr["MaMH"] = ds5.Tables[0].Rows[0]["MaMH"];
                dr["TenMH"] = ds5.Tables[0].Rows[0]["TenMH"];
                dr["TacGia"] = ds5.Tables[0].Rows[0]["TacGia"];
                dr["NhaSX"] = ds5.Tables[0].Rows[0]["NhaSX"];
                dr["DonGia"] = double.Parse(ds5.Tables[0].Rows[0]["DonGiaLe"].ToString());
                dr["Khuyenmai"] = float.Parse(ds5.Tables[0].Rows[0]["Khuyenmai"].ToString());
                tbl.Rows.Add(dr);
            }
            for (int i = 0; i < tbl.Rows.Count; i++)
            {
                tbl.Rows[i]["DonGia"] = (int.Parse( tbl.Rows[i]["DonGia"].ToString())).ToString("#,##0");
            }
            if (!IsPostBack)
            {
                dl_sachmoi.DataSource = tbl;
                dl_sachmoi.DataBind();
            }

        }
        protected void dl_sachmoi_ItemCommand(object source, DataListCommandEventArgs e)
        {

            if (e.CommandName == "Dat")
            {
                if (MaTKOL == "")
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Bạn chưa đăng nhập.');", true);
                else
                {
                    DataTable tbl;
                    if (Session["GioHang"] == null)
                    {
                        tbl = new DataTable();
                        tbl.Columns.Add("MaMH", typeof(string));
                        tbl.Columns.Add("TenMH", typeof(string));
                        tbl.Columns.Add("SoLuong", typeof(int));
                        tbl.Columns.Add("DonGia", typeof(double));
                        tbl.Columns.Add("KhuyenMai", typeof(float));
                        tbl.Columns.Add("ThanhTien", typeof(double), "SoLuong*DonGia");
                    }
                    else
                        tbl = (DataTable)Session["GioHang"];
                    bool trungma = true;
                    if (tbl.Rows.Count > 0)
                    {
                        for (int i = 0; i < tbl.Rows.Count; i++)
                        {
                            if (tbl.Rows[i]["MaMH"].ToString() == e.CommandArgument.ToString())
                            {
                                trungma = false;
                                tbl.Rows[i]["SoLuong"] = (int.Parse(tbl.Rows[i]["SoLuong"].ToString().Trim()) + 1).ToString();
                            }
                        }
                    }
                    if (trungma == true)
                    {
                        DataRow dr = tbl.NewRow();
                        dr["MaMH"] = e.CommandArgument.ToString();
                        HyperLink hb_tenmh = (HyperLink)e.Item.FindControl("HyperLink1");
                        dr["TenMH"] = hb_tenmh.Text;
                        dr["SoLuong"] = 1;
                        Label lb_dongia = (Label)e.Item.FindControl("Label2");
                        dr["DonGia"] = double.Parse(lb_dongia.Text.Replace("VNĐ", ""));
                        dr["KhuyenMai"] = KM(e.CommandArgument.ToString());
                        tbl.Rows.Add(dr);
                    }
                    Session["GioHang"] = tbl;
                }
            }
        }
        private float KM(string mamh)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select khuyenmai from mathang where mamh='"+mamh+"'")));
            float km = float.Parse(ds.Tables[0].Rows[0][0].ToString());
            return km;
        }
    }
}